package cn.hc.share.service.impl;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hc.share.dao.AnswerDao;
import cn.hc.share.dao.CommentDao;
import cn.hc.share.entity.Answer;
import cn.hc.share.entity.Comment;
import cn.hc.share.service.CommentDeleteException;
import cn.hc.share.service.CommentSaveException;
import cn.hc.share.service.CommentService;
import cn.hc.share.util.StringUtil;

@Service("commentService")
public class CommentServiceImpl implements CommentService
{
	@Resource
	private CommentDao commentDao;

	@Resource
	private AnswerDao answerDao;

	// 获取评论
	public List<Comment> getComment(String articleId)
	{
		List<Comment> comments = null;
		if (StringUtil.isNotNull(articleId))
		{
			
			comments = commentDao.findCommentsByArticleId(articleId);
			for(Comment comm:comments) {
				List<Answer> answers=null;
				String commentId = comm.getId();
				answers = answerDao.findAnswersByCommentId(commentId);
				comm.setAnswers(answers);
			}
		}
		return comments;
	}

	// 保存评论
	@Transactional
	public Comment saveComment(String articleId, String userId, String commentContent)
	{
		Comment comment = null;
		if (StringUtil.isNotNull(userId) && StringUtil.isNotNull(articleId) && StringUtil.isNotNull(commentContent))
		{
			comment = new Comment();
			comment.setArticleId(articleId);
			comment.setContent(commentContent);
			comment.setUserId(userId);

			String id = UUID.randomUUID().toString();
			Timestamp createTime = new Timestamp(System.currentTimeMillis());
			comment.setId(id);
			comment.setCreateTime(createTime);

			int num = commentDao.addComment(comment);
			if (num != 1)
			{
				throw new CommentSaveException("评论保存失败!");
			}
		}
		return comment;
	}

	// 保存回复信息
	@Transactional
	public Answer saveAnswer(String userId, String commentId, String replyUserId, String answerContent)
	{
		Answer answer = null;
		if (StringUtil.isNotNull(userId) && StringUtil.isNotNull(commentId) && StringUtil.isNotNull(replyUserId)
				&& StringUtil.isNotNull(answerContent))
		{
			answer=new Answer();
			String id = UUID.randomUUID().toString();
			Timestamp createTime = new Timestamp(System.currentTimeMillis());
			answer.setId(id);
			answer.setCommentId(commentId);
			answer.setContent(answerContent);
			answer.setCreateTime(createTime);
			answer.setReplyUserId(replyUserId);
			answer.setUserId(userId);
			
			int num = answerDao.addAnswer(answer);
			if (num != 1)
			{
				throw new CommentSaveException("回复保存失败!");
			}
			
		}
		return answer;
	}

	//删除评论\回复
	@Transactional
	public int removeComments(String id, String type)
	{
		int num=0;
		if("comment".equals(type)) {
			int commentR = commentDao.removeCommentByCommentId(id);
			if(commentR<1) {
				throw new CommentDeleteException("删除评论失败!");
			}
			
			int answersR = answerDao.removeAnswerByCommentId(id);
			if(answersR<1) {
				throw new CommentDeleteException("删除回复失败!");
			}
			num=commentR+answersR;
		}
		if("answer".equals(type)) {
			int answerNum=answerDao.removeAnswerByAnswerId(id);
			if(answerNum<0) {
				throw new CommentDeleteException("删除回复失败!");
			}
			num = answerNum;
		}
		return num;
	}

}















